package com.ruoyi.web.controller.okr;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.OkrInfo;
import com.ruoyi.system.domain.OkrSeats;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bruce on 2021/9/13 17:30
 **/
@Controller
@RequestMapping("/seats")
public class MyOkrSeatsController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IOkrSeatsService okrSeatsService;

    @Autowired
    private IOkrInfoService okrInfoService;

    @Autowired
    private IOkrAlignInfoService okrAlignInfoService;


    private String prefix =  "okr";

    @GetMapping("")
    public String show(Model model){
        SysUser sysUser = ShiroUtils.getSysUser();
        List<Long> userIds = okrSeatsService.selectSeatUsersByUserId(sysUser.getUserId(), id);
        model.addAttribute("userIds", userIds);
        return prefix + "/okrSeats";
    }

    Long id;

    @PostMapping("/getOkrId")
    @ResponseBody
    public void getOkrId(Long id){
        this.id = id;
    }

    @PostMapping("/getUsers")
    @ResponseBody
    public AjaxResult getUsers(@RequestParam("users[]") List<String> users){

        SysUser user = ShiroUtils.getSysUser();
        if (users.get(0).equals("-1")){
            okrSeatsService.deleteSeatsByUserId(user.getUserId(), id);
            return AjaxResult.success("设置席位成功");
        }

        List<SysUser> userList = new ArrayList<>();
        for (String name : users){
            userList.add(userService.selectUserByUserName(name));
        }

        for (SysUser u : userList){
            if (okrAlignInfoService.isAligned(id, u.getUserId()) > 0){
                return AjaxResult.error(u.getUserName() + " ta已经对齐了你的目标，不可设置私密");
            }
        }

        okrSeatsService.deleteSeatsByUserId(user.getUserId(), id);
        inputUsers(users, user);
        return AjaxResult.success("设置席位成功");
    }

    public void inputUsers(List<String> users, SysUser user){
        OkrInfo okrInfo = okrInfoService.selectOkrInfoById(id);
        if (okrInfo.getRowType() == 0){
            List<OkrInfo> krList = okrInfoService.queryOkrInfoListByParentId(okrInfo.getRowId());
            for (OkrInfo kr : krList){
                for (String name : users){
                    SysUser seatUser = userService.selectUserByUserName(name);
                    OkrSeats okrSeats = new OkrSeats();
                    okrSeats.setOkrId(kr.getId());
                    okrSeats.setUserId(user.getUserId());
                    okrSeats.setSeatUserId(seatUser.getUserId());
                    okrSeatsService.insertSeats(okrSeats);
                }
            }
        }
        for (String name : users){
            SysUser seatUser = userService.selectUserByUserName(name);
            OkrSeats okrSeats = new OkrSeats();
            okrSeats.setOkrId(id);
            okrSeats.setUserId(user.getUserId());
            okrSeats.setSeatUserId(seatUser.getUserId());
            okrSeatsService.insertSeats(okrSeats);
        }
    }








}
